package com.example.myapplication.adapter;

import static androidx.constraintlayout.motion.utils.Oscillator.TAG;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.myapplication.R;
import com.example.myapplication.activity.ShopDetailActivity;
import com.example.myapplication.bean.ShopBean;

import java.util.List;

public class ShopAdapter extends BaseAdapter {
    private Context mContext;
    private List<ShopBean> sb1;
    public ShopAdapter(Context context){
        this.mContext=context;
    }
    public void setDate(List<ShopBean> sb1){
        this.sb1=sb1;
        Log.e(TAG, "setDate: "+sb1 );
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return sb1==null?0:sb1.size();
    }

    @Override
    public ShopBean getItem(int position) {
//        Log.e(TAG, "getItem: "+sb1.get(0));
         return sb1 == null ? null : sb1.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder vh;
        if (convertView==null){
            vh=new ViewHolder();

            convertView= LayoutInflater.from(mContext).inflate(R.layout.shop_item,null);
            Log.e(TAG, "getView: "+vh );
            vh.tv_shop_name=(TextView)convertView.findViewById(R.id.tv_shop_name);
            vh.tv_sale_num=(TextView) convertView.findViewById(R.id.tv_sale_num);
            vh.tv_cost=(TextView)convertView.findViewById(R.id.tv_cost);
            vh.tv_feature=(TextView)convertView.findViewById(R.id.tv_feature);
            vh.tv_time=(TextView)convertView.findViewById(R.id.tv_time);
            vh.iv_shop_pic=(ImageView) convertView.findViewById(R.id.iv_shop_pic);
            convertView.setTag(vh);
        }else {
            vh=(ViewHolder) convertView.getTag();

        }
        final ShopBean bean=getItem(position);

        if (bean!=null){
            Log.e(TAG, "getView: bean"+bean);
            vh.tv_shop_name.setText(bean.getShopName());
            vh.tv_sale_num.setText("月售" + bean.getSaleNum());
            vh.tv_cost.setText("起送￥" + bean.getOfferPrice() + " | 配送￥" +
                    bean.getDistributionCost());
            vh.tv_time.setText(bean.getTime());
            vh.tv_feature.setText(bean.getFeature());
            Glide.with(mContext)
                    .load(bean.getShopPic())
                    .error(R.mipmap.ic_launcher)
                    .into(vh.iv_shop_pic);
        }else{
            Log.e(TAG, "我是空的" );
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (bean==null)return;
                Intent intent=new Intent(mContext, ShopDetailActivity.class);
                intent.putExtra("shop",bean);
                mContext.startActivity(intent);

            }
        });
        return convertView;
    }
    class ViewHolder {
        public TextView tv_shop_name, tv_sale_num, tv_cost, tv_feature, tv_time;
        public ImageView iv_shop_pic;
    }
}
